%%%-------------------------------------------------------------------
%% @doc mysqlboy top level supervisor.
%% @end
%%%-------------------------------------------------------------------

-module(mysqlboy_sup).

-behaviour(supervisor).

-export([start_link/0]).

-export([init/1]).

-define(SERVER, ?MODULE).


start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%% sup_flags() = #{strategy => strategy(),         % optional
%%                 intensity => non_neg_integer(), % optional
%%                 period => pos_integer()}        % optional
%% child_spec() = #{id => child_id(),       % mandatory
%%                  start => mfargs(),      % mandatory
%%                  restart => restart(),   % optional
%%                  shutdown => shutdown(), % optional
%%                  type => worker(),       % optional
%%                  modules => modules()}   % optional
init([]) ->
    SupFlags = {one_for_one, 1, 1},

    Pools = application:get_all_env(mysqlboy),
    ChildSpecs = lists:map(
        fun({Name, Args}) ->
            SizeArgs   = proplists:get_value(poolargs, Args),
            WorkerArgs = proplists:get_value(workerargs, Args),
            PoolArgs   = [{name, {local, Name}}, {worker_module, mysqlboy_worker}] ++ SizeArgs,
            poolboy:child_spec(Name, PoolArgs, WorkerArgs)
        end, 
        Pools),

    {ok, {SupFlags, ChildSpecs}}.

%% internal functions
